package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.List;

public class ForumListPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public ForumListPage() {
        super(url);
    }

    /**
     * 点击头像并退出
     */
    public void quit () {
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_logout > span")).click();
    }

    /**
     * 检查论坛列表是否正常显示
     */
    public void checkForumPage () {
        String title = driver.getTitle();
        assert title.equals("DevForum");
        driver.findElement(By.cssSelector("#article_list_board_title"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post"));
        driver.findElement(By.cssSelector("#page-wrapper > footer > div > div > div > ul > li:nth-child(1)"));
        driver.findElement(By.cssSelector("#index_nav_avatar"));
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg"));
        driver.findElement(By.cssSelector("#page-wrapper > footer > div > div > div > ul > li:nth-child(2) > a"));
    }


    public void checkNoLogin () throws InterruptedException {
        driver.get(url);
    }

    /**
     * 判断夜间模式和白天模式是否可行
     */
    public void checkLightAndDark () {
        // 进入帖子详情
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg")).click();
        driver.getCurrentUrl().contains("dark");
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light > svg")).click();
        driver.getCurrentUrl().contains("light");

    }

    public void checkPublishArticle() {
        // 点击发布帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 检查页面元素
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(1) > label > strong"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(3) > label > strong"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.card-body > div:nth-child(2) > label > strong"));

    }

    /**
     * 检查帖子是否显示正常
     */
    public void checkDetailArticle () {
        // 点击进入帖子详情
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        // 检查元素是否存在
        driver.findElement(By.cssSelector("#details_article_createTime"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2) > svg"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(2) > h3"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4) > svg"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3) > svg"));
    }

    public void checkDetailBoard () {
        // 点击板块
        driver.findElement(By.cssSelector("#topBoardList > li.nav-item.active > a"));
        // 检查是否元素
        driver.findElement(By.cssSelector("#article_list_board_title"));
        driver.findElement(By.cssSelector("#article_list_count_board"));
    }



    public void checkMyArticles () {
        // 进入我的帖子
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_profile")).click();
        // 检查该页面的元素是否有误
        driver.findElement(By.cssSelector("#profile_nickname"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(1)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(2)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(3)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-4 > div > div > div > div > h2"));
    }

    public void checkMyCenter (){
        driver.get("http://127.0.0.1:58080/index.html");
        // 进入个人中心
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col-3.d-none.d-md-block.border-end > div > div > a"));
        driver.findElement(By.cssSelector("#settings_nickname"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > h3:nth-child(4)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > h3:nth-child(7)\n"));
        driver.findElement(By.cssSelector("#setting_submit_nickname"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > h3:nth-child(13)"));
    }

    /**
     * 检查站内信元素是否正常
     */
    public void checkMyMessage () {
        // 进入站内信
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        // 检查元素
        driver.findElement(By.cssSelector("#offcanvasEndLabel"));
    }

    /**
     * 回复站内信成功
     * 直接取消
     */
    public void replyMessageSuccess () throws InterruptedException {
        // 重进一下
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        // 判断是否有站内信
        List<WebElement> elements = driver.findElements(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate > a > span:nth-child(2)"));
        if (!elements.isEmpty()) {
            // 拿到这个元素
            WebElement webElement = elements.get(0);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate > a > span:nth-child(2)")));
            // 点击
            webElement.click();
            Thread.sleep(500);
            driver.findElement(By.cssSelector("#btn_index_message_reply > svg")).click();
            // 输入文本
            driver.findElement(By.cssSelector("#index_message_reply_receive_content")).sendKeys("hello");
            // 点击发送
            driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
            // 捕获错误
            Thread.sleep(500);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
            String[] texts = text.split("\n");
            assert texts[texts.length - 1].equals("操作成功"); // 没有输入
        } else {
            System.out.println("没有站内信");
        }
    }


    /**
     * 回复站内信失败
     * 回复空内容
     */
    public void replyMessageFail1 () throws InterruptedException {
        // 判断是否有站内信
        List<WebElement> elements = driver.findElements(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate > a > span:nth-child(2)"));
        if (!elements.isEmpty()) {
            // 拿到这个元素
            WebElement webElement = elements.get(0);
            // 点击
            webElement.click();
            // 等待内容显示出来
            wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#index_message_detail_content")));
            // 点击回复
            driver.findElement(By.cssSelector("#btn_index_message_reply > svg")).click();
            // 点击发送
            driver.findElement(By.cssSelector("#btn_index_send_message_reply")).click();
            // 捕获错误
            Thread.sleep(500);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
            String[] texts = text.split("\n");
            assert texts[texts.length - 1].equals("请输入要回复的内容"); // 没有输入
        } else {
            System.out.println("没有站内信");
        }
    }

    /**
     * 回复站内信失败
     * 直接取消
     */
    public void replyMessageFail () throws InterruptedException {
        // 判断是否有站内信
        List<WebElement> elements = driver.findElements(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate > a > span:nth-child(2)"));
        if (!elements.isEmpty()) {
            // 拿到这个元素
            WebElement webElement = elements.get(0);
            // 点击
            webElement.click();
            // 等待内容显示出来
            wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#index_message_detail_content")));
            // 点击取消
            driver.findElement(By.cssSelector("#index_message_reply_cancel")).click();

        } else {
            System.out.println("没有站内信");
        }
    }
}
